<template>
  <div v-loading="loading" class="container">
    <div class="app-container">
      <!-- 组织架构内容  -->
      <!--data(数据) props(配置label和children) default-expand-all(默认展开所有节点) -->
      <!-- :data="depts"绑定数据 -->
      <!-- :props="defaultProps"设置好属性，规定好字段名  -->
      <!-- default-expand-all (默认展开所有节点) -->
      <el-tree :expand-on-click-node="false" :data="depts" :props="defaultProps" default-expand-all>
        <!-- 布局组件 el-row-el-col -->
        <template v-slot="{ data }">
          <!-- el-row行组件 el-col列组件 -->
          <el-row align="middle" style="width: 100%;height: 40px;" type="flex">
            <el-col>{{ data.name }}</el-col>
            <!-- 列组件中再嵌套行组件，设置对齐方式 -->
            <el-col>
              <el-row type="flex" justify="end">
                <span class="username">{{ data.managerName }}</span>
                <!-- operateDept函数中不写小括号表示默认传入参数，$event表示默认参数，可写可不写 -->
                <!-- vue的参数 @click=test => @click=test($event) -->
                <el-dropdown @command="operateDept($event,data.id)">
                  <span class="username">操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item command="add">添加子部门</el-dropdown-item>
                    <el-dropdown-item command="edit">编辑部门</el-dropdown-item>
                    <el-dropdown-item command="del">删除</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-row>
            </el-col>
          </el-row>
        </template>
      </el-tree>

    </div>
    <!-- 放置弹层 -->
    <!-- 父组件记录点击的id后传递给子组件 -->
    <!-- .sync会默认监听 @update:showDialog = "showDialog = $event" -->
    <!--通过ref获取组件实例，调用获取详情的方法  -->
    <AddDept ref="addDept" :current-node-id="currentNodeId" :show-dialog.sync="showDialog" @updateDept="getDepartment" />
  </div>
</template>
<script>
import { transListToTreeData } from '@/utils/index'
import { getDepartMentAPI } from '@/api'
import { AddDept } from './components'
export default {
  name: 'Department',
  components: {
    AddDept
  },
  data() {
    return {
      showDialog: false,
      depts: [], // 组织架构数据
      defaultProps: {
        label: 'name',
        children: 'children'
      },
      currentNodeId: null // 存储当前点击的id
    }
  },
  created() {
    this.getDepartment()
  },
  methods: {
    async getDepartment() {
      // this.depts = await getDepartMentAPI()
      this.loading = true
      this.depts = transListToTreeData(await getDepartMentAPI(), 0)
      this.loading = false
    },
    // 点击下拉菜单的方法
    async operateDept(type, id) {
      if (type === 'add') {
        // 添加子部门
        this.showDialog = true // 显示弹层
        this.currentNodeId = id
      } else if (type === 'edit') {
        // 编辑场景
        // Props的渲染是异步的
        this.currentNodeId = id // 给data赋值 传递给props
        // nextTick => 微任务 异步 nextTick会等到组件的上一次更新完成之后执行
        // this.$nextTick(() => {
        //   // 回调函数会等到上一次更新完成之后
        //   this.$refs.addDept.getDepartMentDetail()
        // })
        await this.$refs.addDept.getDepartMentDetail(id)
        this.showDialog = true // 编辑场景下显示弹层
      }
    }
  }
}
</script>
<style scoped>
  .app-container {
    padding: 20px 150px;
  }
  .username {
    margin-right: 10px;
    font-size: 14px;
  }
</style>
